package com.jichaoyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jichaoyun.common.enums.ChargeStatus;
import com.jichaoyun.common.model.PagedResult;
import com.jichaoyun.dao.mapper.*;
import com.jichaoyun.model.entity.Realdata;
import com.jichaoyun.model.entity.RealdataCache;
import com.jichaoyun.model.entity.Rechargeinfo;

import com.jichaoyun.dao.mapper.FlowDayMapper;
import com.jichaoyun.service.IRechargeinfoService;
//import com.sun.tools.javac.comp.Todo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

/**
* @author zhen
* @description 针对表【reChargeInfo】的数据库操作Service实现
* @createDate 2023-12-06 19:34:27
*/
@Service
public class RechargeinfoServiceImpl extends ServiceImpl<RechargeinfoMapper, Rechargeinfo>
    implements IRechargeinfoService {

    @Resource
    private RechargeinfoMapper rechargeinfoMapper;

    private StationInfoMapper stationInfoMapper;

    @Autowired
    private StationInfoServiceImpl stationInfoServiceImpl;

    @Autowired
    private RealdataMapper realdataMapper;

    /**
     * 获取总充值金额
     * @return
     */
    @Override
    public BigDecimal getTotalReCharge() {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("sum(re_charge_value) as total");
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return new BigDecimal(0);
        }
        return (BigDecimal) mapList.get(0).get("total");
    }

    /**
     * 获取IC卡充值笔数
     * @return
     */
    @Override
    public Integer getTotalIcReCharge() {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("count(*) as icCount").eq("charge_type","IC卡充值");
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return 0;
        }
        return (Integer) mapList.get(0).get("icCount");
    }

    /**
     * 获取银行转账笔数
     * @return
     */
    @Override
    public Integer getTotalSysReCharge() {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("count(*) as sysCount").eq("charge_type","银行转账");
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return 0;
        }
        return (Integer) mapList.get(0).get("sysCount");
    }

    @Override
    public Integer getTotalAuxReCharge() {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("count(*) as sysCount").eq("charge_type","辅助充值");
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return 0;
        }
        return (Integer) mapList.get(0).get("sysCount");
    }

    /**
     * 获取某年某月总充值金额
     * @param start
     * @param end
     * @return
     */
    @Override
    public BigDecimal getTotalReChargeByMonth(LocalDateTime start, LocalDateTime end) {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("sum(re_charge_value) as total").between("time",start,end);
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return new BigDecimal(0);
        }

        BigDecimal total =  (BigDecimal) mapList.get(0).get("total");
        return total.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 获取某年某月IC卡充值金额
     * @param start
     * @param end
     * @return
     */
    @Override
    public Integer getTotalIcReChargeByMonth(LocalDateTime start, LocalDateTime end) {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("count(*) as icCount").eq("charge_type","IC卡充值").between("time",start,end);
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return 0;
        }
        return (Integer) mapList.get(0).get("icCount");
    }

    /**
     * 获取某年某月银行转账金额
     * @param start
     * @param end
     * @return
     */
    @Override
    public Integer getTotalSysReChargeByMonth(LocalDateTime start, LocalDateTime end) {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("count(*) as sysCount").eq("charge_type","银行转账").between("time",start,end);
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return 0;
        }
        return (Integer) mapList.get(0).get("sysCount");
    }

    /**
     * 获取某年某月辅助充值金额
     * @param start
     * @param end
     * @return
     */
    @Override
    public Integer getTotalAuxReChargeByMonth(LocalDateTime start, LocalDateTime end) {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("count(*) as sysCount").eq("charge_type","辅助充值").between("time",start,end);
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return 0;
        }
        return (Integer) mapList.get(0).get("sysCount");
    }

    /**
     * 获取某站点某月总充值金额
     * @param stationNo
     * @param start
     * @param end
     * @return
     */
    @Override
    public BigDecimal getTotalReChargeByMonthAndStation(Integer stationNo, LocalDateTime start, LocalDateTime end) {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("sum(re_charge_value) as total").between("time",start,end).eq("station_no",stationNo);
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return new BigDecimal(0);
        }
        BigDecimal total =  (BigDecimal) mapList.get(0).get("total");
        return total.divide(new BigDecimal(10000)).setScale(2,BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 获取某站点某月IC卡充值笔数
     * @param stationNo
     * @param start
     * @param end
     * @return
     */
    @Override
    public Integer getTotalIcReChargeByMonthAndStation(Integer stationNo, LocalDateTime start, LocalDateTime end) {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("count(*) as icCount").eq("charge_type","IC卡充值").between("time",start,end)
                .eq("station_no",stationNo);
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return 0;
        }
        return (Integer) mapList.get(0).get("icCount");
    }

    /**
     * 获取某站点某月银行转账笔数
     * @param stationNo
     * @param start
     * @param end
     * @return
     */
    @Override
    public Integer getTotalSysReChargeByMonthAndStation(Integer stationNo, LocalDateTime start, LocalDateTime end) {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("count(*) as sysCount").eq("charge_type","银行转账").between("time",start,end)
                .eq("station_no",stationNo);
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return 0;
        }
        return (Integer) mapList.get(0).get("sysCount");
    }

    /**
     * 获取某站点某月辅助充值笔数
     * @param stationNo
     * @param start
     * @param end
     * @return
     */
    @Override
    public Integer getTotalAuxReChargeByMonthAndStation(Integer stationNo, LocalDateTime start, LocalDateTime end) {
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.select("count(*) as sysCount").eq("charge_type","辅助充值").between("time",start,end)
                .eq("station_no",stationNo);
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(wrapper);
        if(mapList.get(0)==null){
            return 0;
        }
        return (Integer) mapList.get(0).get("sysCount");
    }

    /**
     * 查询某个用户某年某月每天的充值记录
     * @param stationNo
     * @param start
     * @param end
     * @return
     */
    @Override
    public Map<String, Map<String, Object>> selectChargeListByMonthAndStation(Integer stationNo, LocalDateTime start, LocalDateTime end) {
        Map<String,Map<String,Object>> resMap = new HashMap<>();
        Map<String,Object> totalReChargeList = getTotalReChargeListByMonthAndStation(stationNo,start,end);
        resMap.put("每日总充值金额list",totalReChargeList);

        Map<String,Object> icReChargeList = getTotalIcReChargeListByMonthAndStation(stationNo,start,end);
        resMap.put("充值IC卡笔数list",icReChargeList);

        Map<String,Object>  sysReChargeList = getTotalSysReChargeListByMonthAndStation(stationNo,start,end);
        resMap.put("银行转账笔数list",sysReChargeList);

        Map<String,Object>  auxReChargeList = getTotalAuxReChargeListByMonthAndStation(stationNo,start,end);
        resMap.put("辅助充值笔数list",auxReChargeList);

        return resMap;
    }

    /**
     * 查询所有用户某年某月每天的充值记录
     * @param start
     * @param end
     * @return
     */
    @Override
    public Map<String, Map<String, Object>> selectAllChargeListByMonth(LocalDateTime start, LocalDateTime end) {
        Map<String,Map<String,Object>> resMap = new HashMap<>();
        Map<String,Object> totalReChargeList = getTotalReChargeListByMonth(start,end);
        resMap.put("所有用户每日总充值金额list",totalReChargeList);

        Map<String,Object> icReChargeList = getTotalIcReChargeListByMonth(start,end);
        resMap.put("所有用户充值IC卡笔数list",icReChargeList);

        Map<String,Object>  sysReChargeList = getTotalSysReChargeListByMonth(start,end);
        resMap.put("所有用户银行转账笔数list",sysReChargeList);

        Map<String,Object>  auxReChargeList = getTotalAuxReChargeListByMonth(start,end);
        resMap.put("所有用户辅助充值笔数list",auxReChargeList);

        return resMap;
    }

    /**
     * 充值渠道分析
     * @return
     */
    @Override
    public Map<String, Object> selectAllChargeWay() {
        Map<String,Object> resMap = new HashMap<>();

        QueryWrapper<Rechargeinfo> wrapper1 = new QueryWrapper<>();
        wrapper1.select("sum(re_charge_value) as total").eq("charge_type","IC卡充值");
        List<Map<String, Object>> mapList1 = rechargeinfoMapper.selectMaps(wrapper1);
        BigDecimal ic = new BigDecimal(0);
        if(mapList1.get(0)==null){
            resMap.put("IC卡充值",0);
        }else{
            ic = (BigDecimal) mapList1.get(0).get("total");
            resMap.put("IC卡充值",ic.divide(new BigDecimal(10000)).setScale(2, RoundingMode.HALF_UP));
        }

        QueryWrapper<Rechargeinfo> wrapper2 = new QueryWrapper<>();
        wrapper2.select("sum(re_charge_value) as total").eq("charge_type","银行转账");
        List<Map<String, Object>> mapList2 = rechargeinfoMapper.selectMaps(wrapper2);

        BigDecimal sys = new BigDecimal(0);
        if(mapList2.get(0)==null){
            resMap.put("银行转账",0);
        }else {
            sys =  (BigDecimal) mapList2.get(0).get("total");
            resMap.put("银行转账",sys.divide(new BigDecimal(10000)).setScale(2, RoundingMode.HALF_UP));
        }

        QueryWrapper<Rechargeinfo> wrapper3 = new QueryWrapper<>();
        wrapper3.select("sum(re_charge_value) as total").eq("charge_type","辅助充值");
        List<Map<String, Object>> mapList3 = rechargeinfoMapper.selectMaps(wrapper3);

        BigDecimal aux = new BigDecimal(0);
        if(mapList3.get(0)==null){
            resMap.put("辅助充值",0);
        }else {
            aux = (BigDecimal) mapList3.get(0).get("total");
            resMap.put("辅助充值",aux.divide(new BigDecimal(10000)).setScale(2, RoundingMode.HALF_UP));
        }

        resMap.put("IC卡充值比例",ic.multiply(new BigDecimal(100)).divide(sys.add(ic).add(aux), 1, BigDecimal.ROUND_HALF_UP).toString() + '%');
        resMap.put("银行转账比例",sys.multiply(new BigDecimal(100)).divide(sys.add(ic).add(aux),1, BigDecimal.ROUND_HALF_UP).toString()+ '%');
        resMap.put("辅助充值比例",aux.multiply(new BigDecimal(100)).divide(sys.add(ic).add(aux),1, BigDecimal.ROUND_HALF_UP).toString()+ '%');

        return resMap;
    }

    /**
     * 某年某月充值渠道分析
     * @return
     */
    @Override
    public Map<String, Object> selectAllChargeWayByMonth(LocalDateTime start, LocalDateTime end) {
        Map<String,Object> resMap = new HashMap<>();
        QueryWrapper<Rechargeinfo> wrapper0 = new QueryWrapper<>();
        wrapper0.select("sum(re_charge_value) as total").eq("charge_type","辅助充值").between("time",start,end);
        List<Map<String, Object>> mapList0 = rechargeinfoMapper.selectMaps(wrapper0);

        BigDecimal aux = new BigDecimal(0);
        if(mapList0.get(0)==null){
            resMap.put("辅助充值",0);
        }else{
            aux = (BigDecimal) mapList0.get(0).get("total");
            resMap.put("辅助充值",aux.divide(new BigDecimal(10000)).setScale(2, RoundingMode.HALF_UP));
        }

        QueryWrapper<Rechargeinfo> wrapper1 = new QueryWrapper<>();
        wrapper1.select("sum(re_charge_value) as total").eq("charge_type","IC卡充值").between("time",start,end);
        List<Map<String, Object>> mapList1 = rechargeinfoMapper.selectMaps(wrapper1);

        BigDecimal ic = new BigDecimal(0);
        if(mapList1.get(0)==null){
            resMap.put("IC卡充值",0);
        }else{
            ic = (BigDecimal) mapList1.get(0).get("total");
            resMap.put("IC卡充值",ic.divide(new BigDecimal(10000)).setScale(2, RoundingMode.HALF_UP));
        }

        QueryWrapper<Rechargeinfo> wrapper2 = new QueryWrapper<>();
        wrapper2.select("sum(re_charge_value) as total").eq("charge_type","银行转账").between("time",start,end);
        List<Map<String, Object>> mapList2 = rechargeinfoMapper.selectMaps(wrapper2);

        BigDecimal sys = new BigDecimal(0);
        if(mapList2.get(0)==null){
            resMap.put("银行转账",0);
        }else {
            sys =  (BigDecimal) mapList2.get(0).get("total");
            resMap.put("银行转账",sys.divide(new BigDecimal(10000)).setScale(2, RoundingMode.HALF_UP));
        }

        resMap.put("IC卡充值比例",ic.multiply(new BigDecimal(100)).divide(sys.add(ic).add(aux), 1, BigDecimal.ROUND_HALF_UP).toString() + '%');
        resMap.put("银行转账比例",sys.multiply(new BigDecimal(100)).divide(sys.add(ic).add(aux),1, BigDecimal.ROUND_HALF_UP).toString()+ '%');
        resMap.put("辅助充值比例",aux.multiply(new BigDecimal(100)).divide(sys.add(ic).add(aux),1, BigDecimal.ROUND_HALF_UP).toString()+ '%');

        return resMap;
    }

    /**
     * 某用户某年某月充值渠道分析
     * @return
     */
    @Override
    public Map<String, Object> selectAllChargeWayByMonthAndStation(Integer stationNo, LocalDateTime start, LocalDateTime end) {
        Map<String,Object> resMap = new HashMap<>();

        QueryWrapper<Rechargeinfo> wrapper0 = new QueryWrapper<>();
        wrapper0.select("sum(re_charge_value) as total").eq("charge_type","辅助充值")
                .between("time",start,end)
                .eq("station_no",stationNo);
        List<Map<String, Object>> mapList0 = rechargeinfoMapper.selectMaps(wrapper0);

        BigDecimal aux = new BigDecimal(0);
        if(mapList0.get(0)==null){
            resMap.put("辅助充值",0);
        }else{
            aux = (BigDecimal) mapList0.get(0).get("total");
            resMap.put("辅助充值",aux.divide(new BigDecimal(10000)).setScale(2, RoundingMode.HALF_UP));
        }


        QueryWrapper<Rechargeinfo> wrapper1 = new QueryWrapper<>();
        wrapper1.select("sum(re_charge_value) as total").eq("charge_type","IC卡充值")
                .between("time",start,end)
                .eq("station_no",stationNo);
        List<Map<String, Object>> mapList1 = rechargeinfoMapper.selectMaps(wrapper1);

        BigDecimal ic = new BigDecimal(0);
        if(mapList1.get(0)==null){
            resMap.put("IC卡充值",0);
        }else{
            ic = (BigDecimal) mapList1.get(0).get("total");
            resMap.put("IC卡充值",ic.divide(new BigDecimal(10000)).setScale(2, RoundingMode.HALF_UP));
        }

        QueryWrapper<Rechargeinfo> wrapper2 = new QueryWrapper<>();
        wrapper2.select("sum(re_charge_value) as total").eq("charge_type","银行转账")
                .between("time",start,end)
                .eq("station_no",stationNo);
        List<Map<String, Object>> mapList2 = rechargeinfoMapper.selectMaps(wrapper2);

        BigDecimal sys = new BigDecimal(0);
        if(mapList1.get(0)==null) {
            resMap.put("银行转账", 0);
        }else {
            sys =  (BigDecimal) mapList2.get(0).get("total");
            resMap.put("银行转账",sys.divide(new BigDecimal(10000)).setScale(2, RoundingMode.HALF_UP));
        }


        resMap.put("IC卡充值比例",ic.multiply(new BigDecimal(100)).divide(sys.add(ic).add(aux), 1, BigDecimal.ROUND_HALF_UP).toString() + '%');
        resMap.put("银行转账比例",sys.multiply(new BigDecimal(100)).divide(sys.add(ic).add(aux),1, BigDecimal.ROUND_HALF_UP).toString()+ '%');
        resMap.put("辅助充值比例",aux.multiply(new BigDecimal(100)).divide(sys.add(ic).add(aux),1, BigDecimal.ROUND_HALF_UP).toString()+ '%');

        return resMap;

    }

    /**
     * 充值记录查询
     * @param pageNo
     * @return
     */
    @Override
    public PagedResult selectAllChargeInfoListByPage(Integer pageNo) {
        PagedResult pagedResult = new PagedResult();
        IPage<Rechargeinfo> page = new Page<>(pageNo, 10);
        QueryWrapper<Rechargeinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time");
        IPage<Rechargeinfo> rechargeinfoIPage = rechargeinfoMapper.selectPage(page, queryWrapper);
        pagedResult.setPages(rechargeinfoIPage.getPages());
        pagedResult.setTotal(rechargeinfoIPage.getTotal());


        List<HashMap<String, Object> > resMaplist = new ArrayList<>();
        List<Rechargeinfo> rechargeList = rechargeinfoIPage.getRecords();
        for (Rechargeinfo rechargeinfo : rechargeList) {
            HashMap<String, Object> listMap = new HashMap<>();
            listMap.put("站号",rechargeinfo.getStationNo());
            listMap.put("请求编号",rechargeinfo.getId());
            listMap.put("用户名",rechargeinfo.getStationName());
            listMap.put("时间",rechargeinfo.getTime());
            listMap.put("余额",rechargeinfo.getBalance());
            listMap.put("充值金额",rechargeinfo.getReChargeValue());
            listMap.put("备注",rechargeinfo.getRemark());
            listMap.put("状态", rechargeinfo.getStatus());
            resMaplist.add(listMap);
        }
        pagedResult.setRows(resMaplist);

        return pagedResult;
    }

    /**
     * 单个用户充值记录查询
     * @param pageNo
     * @return
     */
    @Override
    public PagedResult selectAllChargeInfoListByPageAndStaion(Integer pageNo, Integer stationNo) {
        PagedResult pagedResult = new PagedResult();
        IPage<Rechargeinfo> page = new Page<>(pageNo, 10);
        QueryWrapper<Rechargeinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("time").eq("station_no",stationNo);
        IPage<Rechargeinfo> rechargeinfoIPage = rechargeinfoMapper.selectPage(page, queryWrapper);
        pagedResult.setPages(rechargeinfoIPage.getPages());
        pagedResult.setTotal(rechargeinfoIPage.getTotal());

//        HashMap<String, Object> listMap = new HashMap<>();
        List<HashMap<String, Object> > resMaplist = new ArrayList<>();
        List<Rechargeinfo> rechargeList = rechargeinfoIPage.getRecords();
        for (Rechargeinfo rechargeinfo : rechargeList) {
            HashMap<String, Object> listMap = new HashMap<>();
            listMap.put("站号",rechargeinfo.getStationNo());
            listMap.put("用户名",rechargeinfo.getStationName());
            listMap.put("时间",rechargeinfo.getTime());
            listMap.put("余额",rechargeinfo.getBalance());
            listMap.put("充值金额",rechargeinfo.getReChargeValue());
            listMap.put("备注",rechargeinfo.getRemark());
            resMaplist.add(listMap);
        }
        pagedResult.setRows(resMaplist);

        return pagedResult;
    }

    /**
     * 获取总充值金额变化率
     * @param year
     * @param month
     * @return
     */
    @Override
    public String getTotalReChargeRate(Integer year, Integer month) {
        LocalDateTime the_start =  LocalDateTime.of(year,month,1,0,0,0);
        LocalDateTime the_end = LocalDateTime.of(year,month,1,0,0,0).plusMonths(1);
        QueryWrapper<Rechargeinfo> w = new QueryWrapper<>();
        w.select("sum(re_charge_value) as total").between("time",the_start,the_end);
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(w);
        BigDecimal the_total;
        if(mapList.get(0)==null) {
             the_total = new BigDecimal(0);
        }else{
             the_total = (BigDecimal) mapList.get(0).get("total");
        }

        LocalDateTime last_start,last_end;
        if (month.equals(LocalDateTime.now().getMonthValue())){
            Integer day = LocalDateTime.now().getDayOfMonth();
            last_start =  LocalDateTime.of(year,month,1,0,0,0).minusMonths(1);
            last_end = LocalDateTime.of(year,month,day,0,0,0).minusMonths(1);
        }else {
            last_start = the_start.minusMonths(1);
            last_end = the_start;
        }

        QueryWrapper<Rechargeinfo> w1 = new QueryWrapper<>();
        w1.select("sum(re_charge_value) as total").between("time",last_start,last_end);
        List<Map<String, Object>> mapList1 = rechargeinfoMapper.selectMaps(w1);

        if(mapList1.get(0)==null){
            return "上月同期无数据";
        }

        BigDecimal last_total = (BigDecimal) mapList1.get(0).get("total");
        return String.format("%.1f",the_total.subtract(last_total).divide(last_total,3,RoundingMode.HALF_UP).floatValue()*100f) + '%';
    }

    /**
     * 获取IC卡充值金额变化率
     * @param year
     * @param month
     * @return
     */
    @Override
    public String getICReChargeRate(Integer year, Integer month) {
        LocalDateTime the_start =  LocalDateTime.of(year,month,1,0,0,0);
        LocalDateTime the_end = LocalDateTime.of(year,month,1,0,0,0).plusMonths(1);
        QueryWrapper<Rechargeinfo> w = new QueryWrapper<>();
        w.select("count(*) as total").between("time",the_start,the_end).eq("charge_type","IC卡充值");
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(w);
        Integer the_total;
        if(mapList.get(0)==null) {
            the_total = 0;
        }else{
            the_total = (Integer) mapList.get(0).get("total");
        }

        LocalDateTime last_start,last_end;
        if (month.equals(LocalDateTime.now().getMonthValue())){
            Integer day = LocalDateTime.now().getDayOfMonth();
            last_start =  LocalDateTime.of(year,month,1,0,0,0).minusMonths(1);
            last_end = LocalDateTime.of(year,month,day,0,0,0).minusMonths(1);
        }else {
            last_start = the_start.minusMonths(1);
            last_end = the_start;
        }

        QueryWrapper<Rechargeinfo> w1 = new QueryWrapper<>();
        w1.select("count(*) as total").between("time",last_start,last_end);
        List<Map<String, Object>> mapList1 = rechargeinfoMapper.selectMaps(w1);
        if(mapList1.get(0)==null){
            return "上月同期无数据";
        }
        if(mapList1.get(0).get("total").equals(0)){
            return "上月同期无数据";
        }
        Integer last_total = (Integer) mapList1.get(0).get("total");

        return String.format("%.1f",(the_total-last_total)*100f/last_total) + '%';
    }

    /**
     * 获取银行转账金额变化率
     * @param year
     * @param month
     * @return
     */
    @Override
    public String getSysReChargeRate(Integer year, Integer month) {
        LocalDateTime the_start =  LocalDateTime.of(year,month,1,0,0,0);
        LocalDateTime the_end = LocalDateTime.of(year,month,1,0,0,0).plusMonths(1);
        QueryWrapper<Rechargeinfo> w = new QueryWrapper<>();
        w.select("count(*) as total").between("time",the_start,the_end).eq("charge_type","银行转账");
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(w);
        Integer the_total;
        if(mapList.get(0)==null) {
            the_total = 0;
        }else{
            the_total = (Integer) mapList.get(0).get("total");
        }

        LocalDateTime last_start,last_end;
        if (month.equals(LocalDateTime.now().getMonthValue())){
            Integer day = LocalDateTime.now().getDayOfMonth();
            last_start =  LocalDateTime.of(year,month,1,0,0,0).minusMonths(1);
            last_end = LocalDateTime.of(year,month,day,0,0,0).minusMonths(1);
        }else {
            last_start = the_start.minusMonths(1);
            last_end = the_start;
        }

        QueryWrapper<Rechargeinfo> w1 = new QueryWrapper<>();
        w1.select("count(*) as total").between("time",last_start,last_end);
        List<Map<String, Object>> mapList1 = rechargeinfoMapper.selectMaps(w1);
        if(mapList1.get(0)==null){
            return "上月同期无数据";
        }
        if(mapList1.get(0).get("total").equals(0)){
            return "上月同期无数据";
        }
        Integer last_total = (Integer) mapList1.get(0).get("total");

        return String.format("%.1f",(the_total-last_total)*100f/last_total) + '%';
    }

    /**
     * 获取辅助充值金额变化率
     * @param year
     * @param month
     * @return
     */
    @Override
    public String getAuxReChargeRate(Integer year, Integer month) {
        LocalDateTime the_start =  LocalDateTime.of(year,month,1,0,0,0);
        LocalDateTime the_end = LocalDateTime.of(year,month,1,0,0,0).plusMonths(1);
        QueryWrapper<Rechargeinfo> w = new QueryWrapper<>();
        w.select("count(*) as total").between("time",the_start,the_end).eq("charge_type","辅助充值");
        List<Map<String, Object>> mapList = rechargeinfoMapper.selectMaps(w);
        Integer the_total;
        if(mapList.get(0)==null) {
            the_total = 0;
        }else{
            the_total = (Integer) mapList.get(0).get("total");
        }

        LocalDateTime last_start,last_end;
        if (month.equals(LocalDateTime.now().getMonthValue())){
            Integer day = LocalDateTime.now().getDayOfMonth();
            last_start =  LocalDateTime.of(year,month,1,0,0,0).minusMonths(1);
            last_end = LocalDateTime.of(year,month,day,0,0,0).minusMonths(1);
        }else {
            last_start = the_start.minusMonths(1);
            last_end = the_start;
        }

        QueryWrapper<Rechargeinfo> w1 = new QueryWrapper<>();
        w1.select("count(*) as total").between("time",last_start,last_end);
        List<Map<String, Object>> mapList1 = rechargeinfoMapper.selectMaps(w1);
        if(mapList1.get(0)==null){
            return "上月同期无数据";
        }
        if(mapList1.get(0).get("total").equals(0)){
            return "上月同期无数据";
        }
        Integer last_total = (Integer) mapList1.get(0).get("total");

        return String.format("%.1f",(the_total-last_total)*100f/last_total) + '%';
    }


    private Map<String, Object> getTotalSysReChargeListByMonth(LocalDateTime start, LocalDateTime end) {
        HashMap<String, Object> hashMap = new LinkedHashMap<>();
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.between("time",start,end).orderByAsc("time")
                .eq("charge_type","银行转账");
        List<Rechargeinfo> rechargeList = rechargeinfoMapper.selectList(wrapper);

        for (int i =0;i<start.toLocalDate().lengthOfMonth();i++){
            hashMap.put(i+1+"日",0);
        }

        for (Rechargeinfo rechargeinfo : rechargeList) {
            String day = rechargeinfo.getTime().getDayOfMonth() + "日";
            Integer old = (Integer) hashMap.get(day);
            hashMap.put(day, old+1);
        }
        return hashMap;
    }

    private Map<String, Object> getTotalIcReChargeListByMonth(LocalDateTime start, LocalDateTime end) {
        HashMap<String, Object> hashMap = new LinkedHashMap<>();
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.between("time",start,end).orderByAsc("time")
                .eq("charge_type","IC卡充值");
        List<Rechargeinfo> rechargeList = rechargeinfoMapper.selectList(wrapper);

        for (int i =0;i<start.toLocalDate().lengthOfMonth();i++){
            hashMap.put(i+1+"日",0);
        }

        for (Rechargeinfo rechargeinfo : rechargeList) {
            String day = rechargeinfo.getTime().getDayOfMonth() + "日";
            Integer old = (Integer) hashMap.get(day);
            hashMap.put(day, old+1);

        }
        return hashMap;
    }

    private Map<String, Object> getTotalAuxReChargeListByMonth(LocalDateTime start, LocalDateTime end) {
        HashMap<String, Object> hashMap = new LinkedHashMap<>();
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.between("time",start,end).orderByAsc("time")
                .eq("charge_type","辅助充值");
        List<Rechargeinfo> rechargeList = rechargeinfoMapper.selectList(wrapper);

        for (int i =0;i<start.toLocalDate().lengthOfMonth();i++){
            hashMap.put(i+1+"日",0);
        }

        for (Rechargeinfo rechargeinfo : rechargeList) {
            String day = rechargeinfo.getTime().getDayOfMonth() + "日";
            Integer old = (Integer) hashMap.get(day);
            hashMap.put(day, old+1);

        }
        return hashMap;
    }

    private Map<String, Object> getTotalReChargeListByMonth(LocalDateTime start, LocalDateTime end) {
        HashMap<String, Object> hashMap = new LinkedHashMap<>();
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.between("time",start,end).orderByAsc("time");
        List<Rechargeinfo> rechargeList = rechargeinfoMapper.selectList(wrapper);

        for (int i =0;i<start.toLocalDate().lengthOfMonth();i++){
            hashMap.put(i+1+"日",new BigDecimal(0));
        }

        for (Rechargeinfo rechargeinfo : rechargeList) {
            String day = rechargeinfo.getTime().getDayOfMonth() + "日";
            BigDecimal old = (BigDecimal) hashMap.get(day);
            hashMap.put(day, old.add(rechargeinfo.getReChargeValue()));
        }
        // 单位转换
        for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
            BigDecimal value = (BigDecimal) entry.getValue();
            value = value.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP);
            entry.setValue(value);
        }
        return hashMap;

    }

    private Map<String, Object> getTotalSysReChargeListByMonthAndStation(Integer stationNo, LocalDateTime start, LocalDateTime end) {
        HashMap<String, Object> hashMap = new LinkedHashMap<>();
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.between("time",start,end).eq("station_no",stationNo).orderByAsc("time")
                .eq("charge_type","银行转账");
        List<Rechargeinfo> rechargeList = rechargeinfoMapper.selectList(wrapper);

        for (int i =0;i<start.toLocalDate().lengthOfMonth();i++){
            hashMap.put(i+1+"日",0);
        }

        for (Rechargeinfo rechargeinfo : rechargeList) {
            String day = rechargeinfo.getTime().getDayOfMonth() + "日";
            Integer old = (Integer) hashMap.get(day);
            hashMap.put(day, old+1);
        }
        return hashMap;

    }

    private Map<String, Object> getTotalAuxReChargeListByMonthAndStation(Integer stationNo, LocalDateTime start, LocalDateTime end) {
        HashMap<String, Object> hashMap = new LinkedHashMap<>();
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.between("time",start,end).eq("station_no",stationNo).orderByAsc("time")
                .eq("charge_type","辅助充值");
        List<Rechargeinfo> rechargeList = rechargeinfoMapper.selectList(wrapper);

        for (int i =0;i<start.toLocalDate().lengthOfMonth();i++){
            hashMap.put(i+1+"日",0);
        }

        for (Rechargeinfo rechargeinfo : rechargeList) {
            String day = rechargeinfo.getTime().getDayOfMonth() + "日";
            Integer old = (Integer) hashMap.get(day);
            hashMap.put(day, old+1);
        }
        return hashMap;

    }

    private Map<String, Object> getTotalIcReChargeListByMonthAndStation(Integer stationNo, LocalDateTime start, LocalDateTime end) {
        HashMap<String, Object> hashMap = new LinkedHashMap<>();
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.between("time",start,end).eq("station_no",stationNo).orderByAsc("time")
                .eq("charge_type","IC卡充值");
        List<Rechargeinfo> rechargeList = rechargeinfoMapper.selectList(wrapper);

        for (int i =0;i<start.toLocalDate().lengthOfMonth();i++){
            hashMap.put(i+1+"日",0);
        }

        for (Rechargeinfo rechargeinfo : rechargeList) {
            String day = rechargeinfo.getTime().getDayOfMonth() + "日";
            Integer old = (Integer) hashMap.get(day);
            hashMap.put(day, old+1);

        }
        return hashMap;

    }

    private Map<String, Object> getTotalReChargeListByMonthAndStation(Integer stationNo, LocalDateTime start, LocalDateTime end) {
        HashMap<String, Object> hashMap = new LinkedHashMap<>();
        QueryWrapper<Rechargeinfo> wrapper = new QueryWrapper<>();
        wrapper.between("time",start,end).eq("station_no",stationNo).orderByAsc("time");
        List<Rechargeinfo> rechargeList = rechargeinfoMapper.selectList(wrapper);

        for (int i =0;i<start.toLocalDate().lengthOfMonth();i++){
            hashMap.put(i+1+"日",new BigDecimal(0));
        }

        for (Rechargeinfo rechargeinfo : rechargeList) {
            String day = rechargeinfo.getTime().getDayOfMonth() + "日";
            BigDecimal old = (BigDecimal) hashMap.get(day);
            hashMap.put(day, old.add(rechargeinfo.getReChargeValue()));

        }
        // 单位转换
        for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
            BigDecimal value = (BigDecimal) entry.getValue();
            value = value.divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP);
            entry.setValue(value);
        }
        return hashMap;
    }


    @Override
    public List<Map<String, Object>> getAccRechargeByUsers(){
        List<Map<String, Object>> resmap;
        //获取当天的第一天
        LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        resmap = rechargeinfoMapper.getAccRechargeByUsers(start, end);
        return resmap;
    }

    @Resource
    private RealdataCacheMapper realdataCacheMapper;

    @Override
    public Map<String, Float> getAccSpendByUsers(){
        Map<String, Float> resmap = new HashMap<>();
        List<Map<String, Object>> accRecharge;
        LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        //获取所有的站点
        accRecharge = rechargeinfoMapper.getAccRechargeByUsers(start, end);
        for (Map<String, Object> recharge : accRecharge) {
            Realdata realdata = realdataMapper.historySelectOne(((Short) recharge.get("station_no")).intValue(), start, start.plusMinutes(4));
            Float start_money=0f;
            if (realdata != null && realdata.getBalance()!=null) {
                start_money = realdata.getBalance().floatValue();
            }else{
                resmap.put(recharge.get("station_name").toString(),0f);
                continue;
            }
            Float end_money=0f;
            RealdataCache realdata2 = realdataCacheMapper.selectRecentOneByStation(((Short) recharge.get("station_no")).intValue());
            if (realdata2 != null && realdata2.getBalance()!=null) {
                end_money = realdata2.getBalance().floatValue();
            }else{
                resmap.put(recharge.get("station_name").toString(),0f);
                continue;
            }
            resmap.put(recharge.get("station_name").toString(),start_money + (recharge.get("acc_recharge")==null? 0:(Float)recharge.get("acc_recharge")) -end_money);
        }

        return resmap;
    }


}




